<?php
namespace app\controller;

use app\BaseController;
use app\model\admin;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;


// 指定允许其他域名访问
header('Access-Control-Allow-Origin:*');
// 响应类型
header('Access-Control-Allow-Methods:*');
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with,content-type');


class Index extends BaseController
{
    /**
     * @Description 注册
     * @Date   2022/1/18 15:37
     */
    public function register()
    {
        $req = request();
        $isajax = $req->isAjax();
        $data =input("post.");
        if ($isajax){
            $da['username'] =  $data['registerUsername'];
            $da['password'] =  md5($data['registerPassword']);
            $da['loginip']  = \request()->ip();
            $da['create_time'] = time();
            $da['update_time'] = time();
            //查找用户名是否与数据库已有用户名相同
            $find = Db::name('admin')->where('username',  $da['username'])->find();
            if ($find == null){
                if($da != ''){
                    $res = admin::create($da);
                    if ($res){
                        $this->success('注册成功!','login/login','1','4');
                    }else{
                        $this->error('注册失败!','login/login','0','4');
                    }
                }
            }else{
                $this->error('用户名已存在!!','login/login','0','4');
            }

        }else{
            return view('login/login');
        }
    }

    /**
     *
    * @Description 登录页面
    * @Date   2022/1/18 11:00
    */
    public function index()
    {
        $req = request();
        $isajax = $req->isAjax();
        $data =input("post.");
        if ($isajax) {
            $da['password'] =  md5($data['loginPassword']);
            //获取数据库数据进行比较
            $da  =Db::name('admin')->where([
                ['username','=',$data['loginUsername']],
                ['password','=',$da['password']],
            ])->find();

            if ($da){
                //相等时进入
                //生成token session 存储token
                $user['token'] = signToken($da['id'],$da['username']);
//                echo json_encode($user['token']);
                //存入缓存中 token
                Cache::set('token',$user['token'],3600);

                if (empty($da['token'])){
                    $da['token'] = $user['token'];
                    $res = Db::name('admin')->where('id',$da['id'])->update(['token'=>$da['token']]);
                }else{
                    $da['token'] = $user['token'];
                    Db::name('admin')->where('id',$da['id'])->update(['token'=>$da['token']]);
                }
               $re =  $da['token'] = $user['token'];
                Session::set("user_name",$data['loginUsername']);
                Session::set("user_id",$da['id']);
                $session_admin = get_config('app.session_admin');
                Session::set($session_admin, $da);
                $res = [
                    'code'=>'1',
                    'token'=> $da['token']
                ];
                return json($res);
            }else{
                $res = [
                    'code'=>0
                ];
                return json($res);
            }
        }
        return view('login/login');
    }

    /**
    * @Description 后台首页
    * @Date   2022/1/18 15:24
    */
    public function first($token='')
    {
        $req = request();
        $isajax = $req->isAjax();
        $data =input("post.");
        if ($isajax) {
            $res = checkToken($token);
                echo json_encode($res);
                die();
        }
        return view('index/index');
    }



}


